package cn.dankal.basiclib.common.sms;

import android.widget.Button;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import api.FgUserServiceFactory;
import api.UserServiceFactory;
import cn.dankal.basiclib.R;
import cn.dankal.basiclib.base.BaseView;
import cn.dankal.basiclib.rx.AbstractDialogSubscriber;
import cn.dankal.basiclib.util.AppUtils;
import cn.dankal.basiclib.util.StringUtil;
import cn.dankal.basiclib.util.ToastUtils;
import cn.dankal.business.pojo.FgResponse;
import cn.dankal.business.pojo.ReasonBean;
import io.reactivex.Flowable;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

/**
 * Date: 2018/8/1.
 * Time: 12:04
 * classDescription:
 *
 * @author fred
 */
public class SmsCodeImpl implements SmsCode {
    private Disposable mDisposable;

    private final BaseView baseView;
    /**
     * 找回密码
     */
    public static final String TYPE_PASS = "pass";
    /**
     * 设置支付密码
     */
    public static final String TYPE_SET_PAY = "set_pay";
    /**
     * 绑定银行卡
     */
    public static final String TYPE_BIND_CARD = "bind_card";


    public SmsCodeImpl(BaseView baseView) {
        this.baseView = baseView;
    }

    private Flowable flowable;

    @Override
    public void getCode(String phone, Button mBtCode, String type) {

        if (type.equals("0")) {//手机号验证
            if (!StringUtil.checkPhone(phone)) {
                ToastUtils.showShort("手机号格式不正确！");
                return;
            }
        } else {//邮箱验证
            if (!StringUtil.checkEmail(phone)) {
                ToastUtils.showShort("邮箱格式不正确！");
                return;
            }
        }

        UserServiceFactory.obtainVerifyCode(phone, type)
                .subscribe(new AbstractDialogSubscriber<ReasonBean>(baseView) {
                    @Override
                    public void onNext(ReasonBean reasonBean) {
                        sendCodeSuccess(mBtCode);
                    }
                });
    }

    @Override
    public void sendCodeSuccess(Button mBtCode) {
        mBtCode.setEnabled(false);
        mBtCode.setBackgroundColor(AppUtils.getApp().getResources().getColor(R.color.config_color_gray_5));
        //倒计时

        flowable = Flowable.intervalRange(1, 60, 0, 1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(aLong -> {
                    mBtCode.setText("重新获取(" + (60 - aLong) + ")");
                })
                .doOnComplete(() -> {
                    mBtCode.setEnabled(true);
                    mBtCode.setBackgroundColor(AppUtils.getApp().getResources().getColor(R.color.colorAccent));
                    mBtCode.setText("获取验证码");
                });
        mDisposable = flowable.subscribe();
    }

    public void release(Button mBtCode) {
        if (mDisposable != null && !mDisposable.isDisposed()) {
            //停止倒计时
            mDisposable.dispose();
            //重新订阅
            mDisposable = flowable.subscribe();
            //按钮可点击
            mBtCode.setEnabled(true);
            mBtCode.setBackgroundColor(AppUtils.getApp().getResources().getColor(R.color.colorAccent));
            mBtCode.setText("获取验证码");
        }

    }

    @Override
    public void onDestory() {
        if (mDisposable != null) mDisposable.dispose();
    }
}
